package com.example.servicecombsaga.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.servicecomb.saga.omega.context.annotations.SagaStart;
import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TransferService {

    @SagaStart(timeout=10)
    public void transferMoney(String from, String to, int amount) throws InterruptedException {
        transferOut(from, amount);
        transferIn(to, amount);
    }

    @Compensable(timeout=5, compensationMethod="cancelTransferOut")
    public void transferOut(String from, int amount) throws InterruptedException {
        log.info("transferOut start");
        Thread.sleep(6000);
        log.info("transferOut end");
    }

    @Compensable(timeout=5, compensationMethod="cancelTransferIn")
    public void transferIn(String from, int amount) {
        log.info("transferIn start");
        log.info("transferIn end");
    }

    public void cancelTransferIn(String from, int amount) {
        log.info("transferIn cancel");
    }


    public void cancelTransferOut(String from, int amount) {
        log.info("transferOut cancel");
    }
}
